const { static } = require("express");

// 文章数据模型
module.exports = class Article extends require("./model") {
  /**
   * 获取全部文章
   */
  static getHot() {
    return new Promise((resolve, reject) => {
      let sql = "SELECT * FROM article";
      this.query(sql)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          console.log(`获取热门推荐失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   * 获取最新文章
   */
  static getList() {
    return new Promise((resolve, reject) => {
      let sql = "SELECT * FROM article ORDER BY TIME DESC";
      this.query(sql)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          console.log(`获取最新文章失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   *  获取文章详情
   * @param {integer} id 文章id
   */
  static getArticleList(id) {
    return new Promise((resolve, reject) => {
      let sql = "SELECT * FROM article WHERE id = ? ORDER BY id DESC LIMIT 1";
      this.query(sql, id)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          console.log(`获取指定文章失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   * 获取指定分类下的文章列表
   * @param {integer} category_id 分类编号
   * @param {integer} hot 热门
   */
  static getListByCategoryId(category_id, hot) {
    return new Promise((resolve, reject) => {
      let sql = "SELECT * FROM article WHERE 1=1";
      sql +=
        category_id != -1 && category_id
          ? ` AND category_id = ${category_id}`
          : "";
      sql += hot != -1 && hot ? ` AND hot=${hot}` : "";
      sql += " ORDER BY TIME DESC";
      this.query(sql)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          console.log(`获取指定分类下文章失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   * 获取标题搜索内容
   * @param {integer} keyword 搜索内容
   */
  static getListByKeyword(keyword) {
    return new Promise((resolve, reject) => {
      let sql =
        "SELECT id,title,content,`time` FROM article WHERE title LIKE ? ORDER BY TIME DESC";
      this.query(sql, `%${keyword}%`)
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          console.log(`获取标题搜索内容失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   * 获取上一篇文章
   * @param {integer} id 当前文章编号
   */
  static getPrevArticle(id) {
    return new Promise((resolve, reject) => {
      let sql =
        "SELECT id,title FROM article WHERE id < ? ORDER BY id DESC LIMIT 1";
      this.query(sql, id)
        .then((res) => {
          resolve(res[0]);
        })
        .catch((err) => {
          console.log(`获取上一篇文章失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   * 获取下一篇文章
   * @param {integer} id 当前文章编号
   */
  static getNextArticle(id, category_id) {
    return new Promise((resolve, reject) => {
      let sql =
        "SELECT id,title FROM article WHERE id > ? ORDER BY id ASC LIMIT 1";
      this.query(sql, id)
        .then((res) => {
          resolve(res[0]);
        })
        .catch((err) => {
          console.log(`获取下一篇文章失败：${err.message}`);
          reject(err);
        });
    });
  }
  // 获取指定文章列表，实现分页功能
  /**
   *
   * @param {integer} start 查询位置
   * @param {integer} size 显示几条文章
   * @param {integer} category_id 查询分类，根据分类查询对应的文章
   * @param {integer} hots 查询热门，根据查询 hots 查找到热门文章，1：热门，0：普通
   */
  static getPage(start, size, category_id, hots) {
    return new Promise((resolve, reject) => {
      let sql = "SELECT * FROM article WHERE 1=1 ";
      sql +=
        category_id != -1 && category_id
          ? `AND category_id = ${category_id}`
          : "";
      sql += hots != -1 && hots ? ` AND hot=${hots}` : "";
      sql += " ORDER BY `time` DESC LIMIT ?,?";
      this.query(sql, [start, size])
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          console.log(`获取指定文章列表失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   *  设置热门推荐
   * @param {integer} id   文章编号
   * @param {integer} hots 热门状态
   */
  static setHot(id, hots) {
    return new Promise((resolve, reject) => {
      let sql = "UPDATE article SET hot = ? WHERE id = ?";
      this.query(sql, [hots, id])
        .then((res) => {
          resolve(res.affectedRows);
        })
        .catch((err) => {
          console.log(`更新热门状态失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   *  上传文章
   * @param {Object} article 文章对象
   */
  static articleAdd(article) {
    return new Promise((resolve, reject) => {
      let sql = "INSERT INTO article SET ?";
      this.query(sql, article)
        .then((res) => {
          resolve(res.insertId);
        })
        .catch((err) => {
          console.log(`添加文章失败：${err.message}`);
          reject(err);
        });
    });
  }
  /**
   * 删除文章
   * @param {integer} id 文章Id
   */
  static delArticle(id) {
    return new Promise((resolve, reject) => {
      let sql = "DELETE FROM article WHERE id = ?";
      this.query(sql, id)
        .then((res) => {
          resolve(res.affectedRows);
        })
        .catch((err) => {
          console.log(`删除文章失败：${err.message}`);
          reject(err);
        });
    });
  }
  // 编辑文章
  static editArticle(article) {
    console.log(article);
    return new Promise((resolve, reject) => {
      let sql =
        "UPDATE article SET title = ?,content = ?,hot = ?,category_id = ?,category_name = ?,description_image_path = ?,`time` = ? WHERE id = ?";
      let {
        title,
        content,
        hot,
        category_id,
        category_name,
        description_image_path,
        time,
        id,
      } = article;
      this.query(sql, [
        title,
        content,
        hot,
        category_id,
        category_name,
        description_image_path,
        time,
        id,
      ])
        .then((res) => {
          resolve(res.affectedRows);
        })
        .catch((err) => {
          console.log(`编辑文章失败：${err.message}`);
          reject(err);
        });
    });
  }
};
